package com.kly.cashmall.base;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.StringRes;

import com.gyf.immersionbar.ImmersionBar;
import com.kly.cashmall.R;
import com.kly.cashmall.framework.module.BaseActivity;
import com.kly.cashmall.utils.system.BarUtils;

/**
 * Created by zhangxianpei
 * on 2019/4/29.
 */
public abstract class BaseBarActivity extends BaseActivity {

    /**
     * 获取action_bar的layout
     */
    protected @LayoutRes
    int getActionBarLayoutId() {
        return R.layout.action_bar_bg;
    }

    @Override
    protected boolean darkMode() {
        return getActionBarLayoutId() == R.layout.action_bar_bg;
    }

    @Override
    protected View onReplaceRootView(@LayoutRes int layoutResID) {
        View rootView = super.onReplaceRootView(layoutResID);
        FrameLayout container = rootView.findViewById(R.id.action_bar_container);
        container.setVisibility(View.VISIBLE);
        View actionBar =
                LayoutInflater.from(getActivity()).inflate(getActionBarLayoutId(), container, false);
        container.addView(actionBar);
        BarUtils.setActionBarLayout(actionBar);
        ImmersionBar.with(this)
                .barAlpha(0f).statusBarDarkFont(true)
                .init();
        return rootView;
    }

    public void setActionVisible(int... ids) {
        for (int i = 0; i < ids.length; i++) {
            bindView(ids[i], true);
        }
    }

    @Override
    protected void onViewCreated(View view) {
        super.onViewCreated(view);
        barActionInit();
    }

    /**
     * 初始化一些常用的事件
     */
    protected void barActionInit() {
        bindView(R.id.action_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    /**
     * 设置标题
     */
    @Override
    public void setTitle(CharSequence charSequence) {
        bindText(R.id.toolbar_title, charSequence);
    }

    public void setTitleColor(int color) {
        ((TextView) bindView(R.id.toolbar_title)).setTextColor(color);
    }

    /**
     * 设置ICON
     */
    public void setIcon(int drawableID) {
        ((ImageView) bindView(R.id.toolbar_log)).setImageResource(drawableID);

    }

    /**
     * 设置背景图片
     */
    public void setToolbarBackgroundDrawable(int drawableID) {
        bindView(R.id.toolbar_background).setBackgroundResource(drawableID);

    }

    /**
     * 设置背景颜色
     */
    public void setToolbarBackgroundColor(int colorID) {
        bindView(R.id.toolbar_background).setBackgroundColor(colorID);

    }

    /**
     * 设置是否显示
     */
    public void setToolbarVisible(boolean visible) {
        bindView(R.id.toolbar_background, visible);

    }

    /**
     * 客服是否显示
     */
    public void setToolbarCall(boolean visible, int drawableID, View.OnClickListener listener) {
        bindView(R.id.action_call, visible);
        bindView(R.id.action_call, listener);
        ((ImageView) bindView(R.id.action_call)).setImageResource(drawableID);
    }

    /**
     * 设置背景颜色
     */
    public void setTitleAndColorAndDrawable(String title, int colorID, int drawableID) {
        bindText(R.id.toolbar_title, title);
        bindView(R.id.toolbar_background).setBackgroundColor(colorID);
        setIcon(drawableID);
    }

    @Override
    public void setTitle(@StringRes int id) {
        setTitle(getString(id));
    }

}

